var btnType = null, operPageCode = null, clickGridData = null;
//页面按钮操作
function buttonEvent(code, type) {
	btnType = type;
	operPageCode = code;
	var bool = pageParam.buttons[code][type].handler(code);
	if (bool == false) {
		return;
	}
	if (type == "save") {
		saveData(code);
	} else if (type == "cancel") {
		cancel();
	}
}
//datagrid行操作
function click_operations(code, type, index) {
	btnType = type;
	operPageCode = code;
	
	var grid = pageParam.grids[code];
	var data = grid.datagrid("getRows");
	clickGridData = data[index];
	var rowData = {};
	var list = pageParam.fields[operPageCode];
	for (var i = 0;i < list.length;i ++) {
		rowData[list[i].fieldName] = clickGridData[list[i].tableName + "_" + list[i].fieldName];
	}
	
	if (type == "remove") {
		removeData(code, clickGridData);
	} else{
		pageParam.operations[code][type].handler(clickGridData, grid, index, rowData);
	}
}
//打开路径
function openUrl(path, params, title, width, height) {
	var url = path;
	if (url.indexOf("http") < 0 && url.indexOf("www") < 0) {
		url = host + url;
	} else if (url.indexOf("www") > -1) {
		url = "http://" + url;
	}
	dialog = baseJs.window(url, {
		title: title, width: width ? width : 520, height: height ? height : 320
	});
}
//打开模块
var dialog = {};
function openModule(code, title, width, height) {
	var param = "?code=" + code + "&type=" + btnType + "&operCode=" + operPageCode;
	if (btnType == "modify") {
		var ids = [];
		var list = pageParam.fields[operPageCode];
		for (var i = 0;i < list.length;i ++) {
			if (list[i].isId == 1) {
				param += "&" + list[i].fieldName + "=" + clickGridData[list[i].tableName + "_" + list[i].fieldName];
				ids.push(list[i].fieldName);
			}
		}
		if (ids.length == 0) {
			alert("未找到主键！");
			return;
		}
		param += "&edit_ids=" + ids.join(",");
	}
	dialog[operPageCode] = baseJs.window(host + "show/index.jsp" + param, {
		title: title, width: width ? width : 520, height: height ? height : 320
	});
}
//保存按钮事件
function saveData(code) {
	var list = pageParam.fields[code];
	var data = {};
	data["code"] = code;
	data["type"] = baseJs.request("type");
	
	for (var i = 0;i < list.length;i ++) {
		if (list[i].isHide == 1 && data.defaultValue != undefined && data.defaultValue != "") {
			continue;
		}
		var value = "";
		if (list[i].isHide != 1 && list[i].type == "date" || list[i].format == "yyyy-MM-dd") {
			value = $("#" + list[i].tableName + "_" + list[i].fieldName).datebox("getValue") + "";
		} else {
			value = $("#" + list[i].tableName + "_" + list[i].fieldName).val() + "";
		}
		value = value.replace(/\ /g, "");
		try {
			$("#" + list[i].tableName + "_" + list[i].fieldName).val(value);
		} catch (e) {
		}
		
		if (list[i].isNotNull == 1 && value == "") {
			//如果是新增、并且是主键就不做为空判断
			if (!(data["type"] == "add" && list[i].isId == 1)) {
				alert(list[i].name + "不能为空！");
				return;
			}
		}
		if (list[i].isNumber == 1 && isNaN(value * 1)) {
			alert(list[i].name + "为数字类型！");
			return;
		}
		data[list[i].tableName + "_" + list[i].fieldName] = value;
	}
	load = baseJs.loadding();
	$.post(host + "showAction/saveData.html", data, function(result) {
		if (result.status == "ok") {
			baseJs.alert(result.msg);
			var code = baseJs.request("operCode");
			if (!code) {
				code = operPageCode;
			}
			//回调函数
			try{
				pageParam.buttons[operPageCode][btnType].success(data, result);
			} catch (e) {}
			cancel();
		} else {
			alert(result.msg);
		}
		load.close();
	}, "json");
}
//重置按钮，重置页面数据
function clearData(code) {
	var type = baseJs.request("module_type");
	if (type == "modify") {
		initEditData(code, pageParam.data[json.code]);
	} else {
		var list = pageParam.fields[code];
		for (var i = 0;i < list.length;i ++) {
			if (list[i].isHide == 1 && data.defaultValue != undefined && data.defaultValue != "") {
				continue;
			}
			var value = "";
			if (data.defaultValue != undefined && data.defaultValue != "") {
				value = data.defaultValue;
			}
			if (list[i].isHide != 1 && list[i].type == "date" || list[i].format == "yyyy-MM-dd") {
				$("#" + list[i].tableName + "_" + list[i].fieldName).datebox({value: value});
			} else {
				$("#" + list[i].tableName + "_" + list[i].fieldName).val(value);
			}
		}
	}
}
//删除数据按钮
function removeData(code, data) {
	baseJs.confirm("确定提示", "确定删除此数据？", function(r) {
		if (r) {
			load = baseJs.loadding();
			data["code"] = code;
			$.post(host + "showAction/deleteData.html", data, function(result) {
				if (result.status == "ok") {
					baseJs.alert(result.msg);
					//回调函数
					try{
						pageParam.buttons[operPageCode][btnType].success(data, result);
					} catch (e) {
					}
					reloadGrid(operPageCode);
				} else {
					alert(result.msg);
				}
				load.close();
			}, "json");
		}
	});
}
//取消按钮事件
function cancel() {
	baseJs.closeWindow();
}